//
//  LoginMoreOptionsView.swift
//  ezyform
//
//  Created by Moennig Q on 21/7/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class LoginMoreOptionsView: UIView {
    var startRecognizationClosure: (()->Void)?
    var moreOptionClosure: (()->Void)?
    lazy var emailLabel: BaseLabel = {
        let label = BaseLabel()
        label.textColor = UIColor(hexStr: "#292D32B3")
        label.setFont(R.font.interMedium(size: 15))
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    lazy var optionsMenuView: LoginOptionsMenuView = {
        let view = LoginOptionsMenuView()
        return view
    }()
    lazy var gestureLockView: GestureLockView = {
        let view = GestureLockView()
        return view
    }()
    lazy var touchOrFaceBtn: UIButton = {
        let btn = UIButton()
        return btn
    }()
    lazy var clickHearToStartBtn: BaseButton = {
        let btn = BaseButton()
        btn.setTitle("Click here to start", for: UIControl.State.normal)
        btn.setTitleColor(UIColor(hexStr: "#788598"), for: UIControl.State.normal)
        btn.setFont(R.font.sfProTextRegular(size: 15))
        return btn
    }()
    lazy var tipLabel: BaseLabel = {
        let label = BaseLabel()
        label.textColor = UIColor(hexStr: "#ED3E3E")
        label.setFont(R.font.interMedium(size: 15))
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpView() {
        self.backgroundColor = .white
        
        //email
        self.addSubview(emailLabel)
        emailLabel.snp.makeConstraints { make in
            make.top.equalTo(100)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.greaterThanOrEqualTo(0)
            make.centerX.equalToSuperview()
        }
        //gesture view
        self.addSubview(gestureLockView)
        gestureLockView.snp.makeConstraints { make in
            make.top.equalTo(emailLabel.snp.bottom).offset(30)
            make.left.right.equalToSuperview()
            make.height.equalTo(self.gestureLockView.gestureLockViewHeight)
        }
        //
        self.addSubview(touchOrFaceBtn)
        touchOrFaceBtn.snp.makeConstraints { make in
            make.center.equalTo(gestureLockView)
            make.width.height.equalTo(100)
        }
        
        //tipLabel
        self.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.top.equalTo(gestureLockView.snp.bottom)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.greaterThanOrEqualTo(0)
            make.centerX.equalToSuperview()
        }
//        self.addSubview(clickHearToStartBtn)
//        clickHearToStartBtn.snp.makeConstraints { make in
//            make.top.equalTo(touchOrFaceBtn.snp.bottom)
//            make.centerX.equalTo(touchOrFaceBtn)
//            make.height.equalTo(50)
//            make.width.greaterThanOrEqualTo(0)
//        }
        //more option
        let moreOptionBtn = BaseButton()
        moreOptionBtn.setTitle(R.string.localizable.moreOptions(), for: UIControl.State.normal)
        moreOptionBtn.setTitleColor(UIColor(hexStr: "#379657"), for: UIControl.State.normal)
        moreOptionBtn.setFont(R.font.interMedium(size: 15))
        self.addSubview(moreOptionBtn)
        moreOptionBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-120)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerX.equalToSuperview()
        }
        moreOptionBtn.addTarget(self, action: #selector(moreOptionBtnClick), for: UIControl.Event.touchUpInside)
        touchOrFaceBtn.addTarget(self, action: #selector(touchOrFaceBtnClick), for: UIControl.Event.touchUpInside)
        clickHearToStartBtn.addTarget(self, action: #selector(clickHearToStartBtnClick), for: UIControl.Event.touchUpInside)
        self.touchOrFaceBtn.isHidden = true
        self.gestureLockView.isHidden = true
        
        
    }
    @objc func touchOrFaceBtnClick(){
        self.startRecognizationClosure?()
    }
    @objc func clickHearToStartBtnClick(){
        self.startRecognizationClosure?()
    }
    
    @objc func moreOptionBtnClick(){
        self.moreOptionClosure?()
    }
    func showMenuView(){
        self.optionsMenuView.initMenuUI()
        self.addSubview(self.optionsMenuView)
        optionsMenuView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    func hideMenuView(){
        if self.optionsMenuView.superview != nil{
            self.optionsMenuView.removeFromSuperview()
        }
    }
    func dealLoginMethod(_ loginMethod: LoginMethodOperation){
        self.tipLabel.text = ""
        self.touchOrFaceBtn.isHidden = true
        self.clickHearToStartBtn.isHidden = true
        self.gestureLockView.isHidden = true
        switch loginMethod {
        case .gesture:
            self.gestureLockView.isHidden = false
        case .touchID:
            self.touchOrFaceBtn.isHidden = false
            self.touchOrFaceBtn.setImage(UIImage(named:"login_options_touchid_disabled"), for: UIControl.State.normal)
            self.clickHearToStartBtn.isHidden = false
        case .faceID:
            self.touchOrFaceBtn.isHidden = false
            self.touchOrFaceBtn.setImage(UIImage(named:"login_options_faceid_disabled"), for: UIControl.State.normal)
            self.clickHearToStartBtn.isHidden = false
        default:
            break
        }
    }
    //处理生物识别是否可以重启
    func dealRestartBiometry(_ abled: Bool){
        if abled {//
            self.clickHearToStartBtn.isHidden = false
        }else{
            self.clickHearToStartBtn.isHidden = true
        }
    }
}

